package org.hxyjs.dao.impl;

import org.hxyjs.dao.PraiseResDao;
import org.hxyjs.entity.PraiseRes;
import org.hxyjs.mapper.PraiseResMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("praiseResDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class PraiseResDaoImpl implements PraiseResDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private PraiseResMapper praiseResMapper;

    @Override
    public List<PraiseRes> getPraiseResByPage(PageInfo pageInfo) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", pageInfo.getCurrPage());
        map.put("pageSize", pageInfo.getPageSize());
        int itemCount = (int) praiseResMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return praiseResMapper.getPraiseResByPage(map);
    }


    public List<PraiseRes> getAllPraiseRes() throws Exception {
        return praiseResMapper.selectByExample(null);
    }

    @Override
    public PraiseRes getPraiseResByUrid(String urid) throws Exception {
        return praiseResMapper.selectByPrimaryKey(urid);
    }


    @Override
    public Integer deletePraiseResByUrid(String urid) {
        return praiseResMapper.deleteByPrimaryKey(urid);
    }

    @Override
    public Integer updatePraiseResByUrid(PraiseRes praiseRes) {

        return praiseResMapper.updateByPrimaryKey(praiseRes);
    }

    @Override
    public Integer addPraiseRes(PraiseRes praiseRes) {
        praiseRes.setUrid(initId());
        return praiseResMapper.insertSelective(praiseRes);
    }

    public String initId() {
        String maxUrid = praiseResMapper.getMaxUrid();
        return GenNumTools.initId("0", 3, maxUrid);
    }

}